package com.sharainy.service.impl;


import com.sharainy.dao.RechargeMapper;
import com.sharainy.pojo.Recharge;
import com.sharainy.service.RechargeService;
import com.sharainy.util.HttpUtil;
import com.sharainy.util.PayConfigUtil;
import com.sharainy.util.PayToolUtil;
import com.sharainy.util.XMLUtil4;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;

@Service
public class RechargeServiceImpl implements RechargeService {
    @Autowired
    private RechargeMapper rechargeMapper;
    @Override
    public Recharge selectRecharge(int merchantId) {
        return rechargeMapper.selectRecharge(merchantId);
    }

    @Override
    public int addRecharge(Recharge recharge) {
         rechargeMapper.addRecharge(recharge);
        return recharge.getId();
    }

    @Override
    public List<Recharge> selectRechargeById(Map<String, String> map) {
        return rechargeMapper.selectRechargeById(map);
    }

    @Override
    public Recharge selectByRechargeId(int id) {
        return rechargeMapper.selectByRechargeId(id);
    }

    @Override
    public String weixinPay(String recharge, String merchantId, String timestamp) throws Exception {

        String out_trade_no =merchantId+"-"+timestamp.substring(0,10); //订单号 （调整为自己的生产逻辑）
        int re = Math.round( Float.parseFloat(recharge)*100);
        // 账号信息
        String appid = PayConfigUtil.APP_ID;  // appid
        // String appsecret = PayConfigUtil.APP_SECRET; // appsecret
        String mch_id = PayConfigUtil.MCH_ID; // 商业号
        String key = PayConfigUtil.API_KEY; // key

        String currTime = PayToolUtil.getCurrTime();
        String strTime = currTime.substring(8, currTime.length());
        String strRandom = PayToolUtil.buildRandom(4) + "";
        String nonce_str = strTime + strRandom;

        // 获取发起电脑 ip
        String spbill_create_ip = PayConfigUtil.CREATE_IP;
        // 回调接口
        String notify_url = PayConfigUtil.NOTIFY_URL;
        String trade_type = "NATIVE";

        SortedMap<Object, Object> packageParams = new TreeMap<Object, Object>();
        packageParams.put("appid", appid);
        packageParams.put("mch_id", mch_id);
        packageParams.put("nonce_str", nonce_str);
        packageParams.put("body", merchantId+"预付费充值");  //（调整为自己的名称）
        packageParams.put("out_trade_no", out_trade_no);
        packageParams.put("total_fee", re+""); //价格的单位为分
        packageParams.put("spbill_create_ip", spbill_create_ip);
        packageParams.put("notify_url", notify_url);
        packageParams.put("trade_type", trade_type);

        String sign = PayToolUtil.createSign("UTF-8", packageParams,key);
        packageParams.put("sign", sign);

        String requestXML = PayToolUtil.getRequestXml(packageParams);
        System.out.println(requestXML);

        String resXml = HttpUtil.postData(PayConfigUtil.UFDODER_URL, requestXML);

        System.out.println("resXml:"+resXml);
        Map map = XMLUtil4.doXMLParse(resXml);
        String urlCode = (String) map.get("code_url");
        System.out.println("urlCode:"+urlCode);
        return urlCode;
    }

    @Override
    public Recharge selectNewRecharge(String merchantId) {
        return rechargeMapper.selectNewRecharge(merchantId);
    }

    @Override
    public Recharge selectRechargeByTimeAndId(Map<String, String> map) {
        return rechargeMapper.selectRechargeByTimeAndId(map);
    }
}
